<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$return=array();

if($_RQ['op']=='get'){
	$merchid=intval($_RQ['merchid']);
	if($merchid || !$_RQ['ismerch']){
		$this->session('merchid',$merchid);
	}
	if($_RQ['ismerch']){
		$merchid=$_SESSION['merchid'];
		$this->session('merchid',$merchid);
	}
	$return['item']=array('merchid'=>$merchid);
	$return['item']['excelurl']=SITEROOT.'/addons/jy_tbassistant/resource/test.csv';
	$return['item']['zipurl']=SITEROOT.'/addons/jy_tbassistant/resource/test.zip';
	$return['htmls']=array (
		  'type' => 'edit',
		  'post' => 'post',
		  'edit_tab' => '淘宝CSV上传助手',
		  'edits' => array (
		    array (
		      'title' => '淘宝CSV上传助手',
		      'list' => array (
				array ('type' => 'tip','title' => '<red>[温馨提示]：在导入过程中，请不要进行任何操作!</br>功能介绍：可将淘宝助理以及其他途径获取的淘宝商品CSV文件快速上传至商城,节约您的大量时间!
				</br>使用方法：</br>
				1、先淘宝助手5下载《淘宝数据包》；</br>
				2. 将您获取到的CSV文件,并且上传，或上传添加好商品信息的《CSV示例文件》</br>
				3. 将配套的图片文件包压缩为Zip格式压缩包并且上传(图片需在压缩包根目录下)</br>
				4. 最后，确认上传即可
				</red>'),
				array ('name' => 'excelurl','type' => 'down','title' => 'CSV示例文件下载'),
				array ('name' => 'zipurl','type' => 'down','title' => 'Zip示例文件下载'),
				array ('name' => 'excel','type' => 'upfile','title' => '上传CSV'),
				array ('name' => 'zip','type' => 'upfile','title' => '上传ZIP'),
				array ('name' => 'isupimg','type' => 'switch','title' => '商品详情里面的图片需要保存到本地？'),
			 ),
		    ),
		  ),
	);
	//获取分类
	$params=array('uniacid'=>$_SESSION['uniacid'],'parentid'=>0);
	$return['pcate']=pdo_getall('jy_weishop_goods_group',$params);
	$c=array();
	foreach($return['pcate'] as $v){
		$n=$v['title'];
		$son=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$v['id']));
		if($son){
			foreach($son as $s){
				$d=$s['id'];
				$c[$n][$d]=$s['title'];
			}
		}else{
			$c[$n][0]='';
		}
	}
	$return['cid']=$c;
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$merchid=$post['merchid'];
	if(!$post['zip']){
		//exi('请先上传zip文件！','error');
	}
	if(!$post['excel']){
		exi('请先上传CSV文件！','error');
	}
	if($post['zip']){
		$zip=cfc('files')->local($post['zip']);
	}
	$this->session('isupimg',$post['isupimg']);
	$excel=cfc('files')->local($post['excel']);
	$s = file_get_contents($excel); //读取文件到变量
	$s = iconv('UTF-16LE', 'utf-8', substr($s,2));//转码，substr($s,2)的作用是去掉 BOM 头 FFFE
	//有的第三方数据包很不规范，控制符没有用空双引号
	//可能导致列数不对应，在此加上空双引号
	$s = str_replace("\t\t", "\t\"\"\t", $s);
	$s = str_replace("\t\t", "\t\"\"\t", $s);
	$csv_data = $this->model('taobaocsv')->tb_str_getcsv($s, "\t");//读取到数组
	cfc('files')->source_del($post['excel']);
	$this->model('taobaocsv')->get_taobaocsvitems($csv_data,$zip);	
	exi('上传文件成功，开始采集','','jy_tbassistant/taobaocsv/updata/');
}
if($_RQ['op']=='updata'){
	$merchid=$this->session('merchid');
	$num = intval($_RQ['num'])?:0;
	$items=cache_read('taobaocsv'.$_SESSION['uid'],'jy_tbassistant');
	$items=json_decode($items,true);
	$totalnum = count($items);
	$this->model('taobaocsv')->save_taobaocsv_goods($items[$num],$merchid);
	if ($totalnum <= $num + 1) {
			cache_delete('taobaocsv'.$_SESSION['uid'],'jy_tbassistant');
			exi('商品全部导入成功！','','jy_tbassistant/goods/');
	}else{
			$num++;
			exi('已成功导入第【'.$num.'】个商品！！','','jy_tbassistant/taobaocsv/updata/num='.$num);
	}	
}
?>